<template>
  <div style="background: #f5f5f5; padding-bottom: 20px">
    <img class="vip-banner" src="@/assets/images/vip.jpg" alt="" />
    <div class="vip-box">
      <h2>基本信息</h2>
      <div class="vip-group">
        <van-cell title="姓名" :value="userInfo.realse_name" />
        <van-cell title="身份证号码" :value="userInfo.id_card" />
        <van-cell title="银行名称" :value="userInfo.bank_name" />
        <van-cell title="银行卡号" :value="userInfo.bank_card" />
        <!-- <van-cell title="会员价格">
          <template #default>
            <h3>
              ￥<span>{{ newMoney }}元</span>
            </h3>
          </template>
        </van-cell> -->
      </div>
    </div>
    <van-button
      class="vip-group-btn"
      round
      block
      type="info"
      url="https://ykf-webchat.7moor.com/wapchat.html?accessId=1cb1fb90-3ab5-11eb-b9a7-c7d93f891c90&fromUrl=&urlTitle=&language=ZHCN"
      color="linear-gradient(to right, #ebb15b, #f6c775)"
    >
      在线客服
    </van-button>
    <van-button
      class="vip-group-btn"
      round
      block
      type="info"
      @click="show = true"
      color="linear-gradient(to right, #ebb15b, #f6c775)"
    >
      下一步
    </van-button>
    <van-button class="vip-group-btn" round block @click="$utils.replace('/')">
      返回首页
    </van-button>
    <van-popup
      class="pay-popup"
      closeable
      v-model="show"
      round
      position="bottom"
    >
      <div class="pay-banner">
        <h2>{{ info && info.people }}人已购会员并成功下款</h2>
        <div class="pay-swipe">
          <h3>VIP会员</h3>
          <van-swipe
            vertical
            class="notice-swipe"
            :autoplay="3000"
            :show-indicators="false"
            v-if="dataInfo"
          >
            <van-swipe-item
              v-for="(item, index) in dataInfo.mockData"
              :key="index"
              >{{ item.name }} 成功下款{{ item.quota }}元</van-swipe-item
            >
          </van-swipe>
        </div>
      </div>
      <div class="pay-main">
        <h2>提现管理</h2>
        <!--  <ul class="pay-menu">
          <li :class="{ active: isActive == 0 }" @click="changeTab(info, 0)">
            <h2>月卡</h2>
            <h3>￥{{ info && info.monthNew }}</h3>
          </li>
          <li :class="{ active: isActive == 1 }" @click="changeTab(info, 1)">
            <h2>季卡</h2>
            <h3>￥{{ info && info.seasonNew }}</h3>
          </li>
          <li :class="{ active: isActive == 2 }" @click="changeTab(info, 2)">
            <h2>年卡</h2>
            <h3>￥{{ info && info.yearNew }}</h3>
          </li>
        </ul> -->
        <!-- <h3>
          <span>未下款可赔付最高额度</span
          ><span>{{ info && info.compensate }}元</span>
        </h3> -->
        <div class="pay-desc">
          <van-cell title="借款用途">
            <template #default><span>日常消费</span></template>
          </van-cell>
          <!--  <van-cell title="支付价格">
            <template #default
              ><p>
                <span>{{ newMoney }}</span
                >元/{{ cardText }}
              </p></template
            >
          </van-cell> -->
          <van-cell class="cell-item" style="padding: 0">
            <van-field
              class="login-form-item"
              clearable
              v-model="smsCode"
              style="align-content: center"
              ref="smsCode"
              placeholder="请输入短信验证码"
            >
              <template #button>
                <van-button
                  size="small"
                  color="linear-gradient(to right, #ebb15b, #f6c775)"
                  @click="getPaySms"
                  type="primary"
                  >{{ codeText }}</van-button
                >
              </template>
            </van-field>
          </van-cell>
          <van-cell class="cell-item-vip" title="会员权益服务费">
            <template #default> <p>299元</p></template>
          </van-cell>
          <van-button
            class="vip-group-btn"
            round
            @click="pay(type)"
            block
            type="info"
            color="linear-gradient(to right, #ebb15b, #f6c775)"
          >
            立即提现
          </van-button>
        </div>
      </div></van-popup
    >

    <div v-html="payHtml"></div>
  </div>
</template>

<script>
import { index, settingInfo } from '@/api/index'
import { payment, paySms, queryOrder } from '@/api/user'
import { mapState } from 'vuex'
const text = '获取验证码'
export default {
  computed: {
    ...mapState({
      userInfo: (state) => state.user.userInfo,
    }),
  },
  data() {
    return {
      isActive: 0,
      show: false,
      payHtml: undefined,
      dataInfo: null,
      info: null,
      smsCode: undefined,
      codeText: text,
      historyMoney: undefined,
      newMoney: undefined,
      type: 'monthNew',
      cardText: '月',
      orderNO: undefined,
    }
  },
  created() {
    this.$nextTick(async () => {
      this.dataInfo = await index()
      this.info = await settingInfo()
      this.historyMoney = this.info.month
      this.newMoney = this.info.monthNew
      if (this.userInfo.is_vip == 1) {
        this.$utils.replace('/')
      }
    })
  },
  methods: {
    getPaySms() {
      if (this.codeText === text) {
        paySms({
          type: this.type,
        }).then((res) => {
          console.log(res)
          console.log(this.codeText)
          console.log(text)
          if (res.status !== 0) {
            this.$toast({
              position: 'bottom',
              message: res.msg,
            })
            return
          }
          this.orderNO = res.order_no
          let time = 60
          this.timer = setInterval(() => {
            time--
            this.codeText = '（' + time + '秒）重发'
            if (time === 0) {
              clearInterval(this.timer)
              this.codeText = text
            }
          }, 1000)
        })
      }
    },

    changeTab(info, index) {
      this.isActive = index
      if (index == 0) {
        this.historyMoney = info.month
        this.newMoney = info.monthNew
        this.type = 'monthNew'
        this.cardText = '月'
      }
      if (index == 1) {
        this.historyMoney = info.season
        this.newMoney = info.seasonNew
        this.type = 'seasonNew'
        this.cardText = '季'
      }
      if (index == 2) {
        this.historyMoney = info.year
        this.newMoney = info.yearNew
        this.type = 'yearNew'
        this.cardText = '年'
      }
    },
    queryPay() {
      queryOrder({ order_no: this.orderNO }).then((res) => {
        if (res.status === 1) {
          this.$toast({
            position: 'bottom',
            message: res.msg,
          })
        } else if (res.status === 4) {
          // 支付处理中状态，查询
          setTimeout(this.queryPay, 1000)
        } else if (res.status === 0) {
          // 支付成功需要跳转到的位置
          this.$toast({
            position: 'bottom',
            message: res.msg,
          })
        }
      })
    },
    pay(type) {
      if (!this.smsCode) {
        this.$toast({
          position: 'bottom',
          message: '短信验证码为空',
        })
        this.$refs.smsCode.focus()
        return true
      }

      if (!this.orderNO) {
        this.$toast({
          position: 'bottom',
          message: '短信验证码无效',
        })
        this.$refs.smsCode.focus()
        return true
      }

      payment({ code: this.smsCode, order_no: this.orderNO }).then((res) => {
        if (res.status === 3) {
          this.queryPay()
          this.$utils.push('/')
        }
        console.log(res)
      })
      // payent(type).then(async res => {
      //   this.payHtml = res
      //   setTimeout(() => {
      //     document.forms['alipaysubmit'].submit()
      //   }, 500)
      // })
    },
  },
}
</script>

<style lang="less" scoped>
.cell-item {
  span{
    font-size: 14px;
  }
  ::v-deep {
    .van-field__control {
      font-size: 14px;
      color: #969799;
    }
  }
}
.cell-item-vip {
  ::v-deep {
    p {
      font-size: 12px;
      color: #d1d2d4;
    }
    span {
      font-size: 12px;
      color: #d1d2d4;
    }
  }
}
#alipaysubmit {
  input {
    opacity: 0;
  }
}
.vip-banner {
  display: block;
  width: 100%;
  /*  height: 180px; */
}
.vip-box {
  width: 330px;
  margin: 20px auto;
  padding: 10px;
  background: #fff;
  border-radius: 10px;
  h2 {
    margin: 10px;
    font-size: 18px;
    color: #444;
    font-weight: bold;
  }
  .vip-group {
    h3,
    h4 {
      display: inline-block;
      font-size: 14px;
      color: #fa1037;
    }
    h3 {
      margin-right: 10px;
      span {
        font-size: 16px;
        font-weight: bold;
      }
    }
  }
}
.vip-group-btn {
  width: 300px;
  margin: 0 auto;
  margin-top: 20px;
}
.pay-popup {
  background: none;
}
::v-deep .van-popup__close-icon--top-right {
  top: 100px;
}
.pay-banner {
  width: 300px;
  margin: 0 auto;
  h2 {
    color: #fff;
    text-align: center;
  }
  .pay-swipe {
    display: flex;
    justify-content: flex-start;
    background: #000;
    padding: 6px 10px;
    border-radius: 6px;
    margin-top: 10px;
    margin-bottom: 20px;
    h3 {
      min-width:80px;
      padding: 4px;
      color: #fff;
      text-align: center;
      font-size: 14px;
      background: linear-gradient(#ebb15d, #f3c36f);
    }
    .notice-swipe {
      width: 200px;
      margin-left: 10px;
      height: 20px;
      line-height: 20px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      div {
        font-size: 14px;
        color: #fff;
      }
    }
  }
}
.pay-main {
  background: #fff;
  border-radius: 20px 20px 0 0;
  padding: 0 0 20px 0;
  & > h3 {
    width: 330px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    background: #fef4f5;
    margin-top: 20px;
    padding: 10px;
    color: #fe173b;
    span {
      font-weight: bold;
    }
  }
  & > h2 {
    text-align: center;
    color: #fa2044;
    padding: 20px 0;
    font-size: 16px;
    font-weight: bold;
  }
  .pay-menu {
    width: 330px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    li {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 104px;
      height: 40px;
      background: url('../../assets/images/pay-bg-n.png') no-repeat;
      background-size: 100% 100%;
      color: #fff;
      h2,
      h3 {
        font-size: 14px;
      }
      h2 {
        color: #cdcdcd;
        margin-left: 4px;
      }
      h3 {
        color: #fff;
        margin-right: 8px;
      }
      &.active {
        background: url('../../assets/images/pay-bg.png') no-repeat;
        background-size: 100% 100%;
        h2 {
          color: #444444;
        }
      }
    }
  }
  .pay-desc {
    width: 330px;
    margin: 0 auto;
    margin-top: 10px;
  }
}
</style>
